package codegenerator.generator;

import java.util.List;

import org.slf4j.Logger;

import baytony.util.StringUtil;
import baytony.util.log.Log;
import codegenerator.CodeGeneratorException;
import codegenerator.ColumnInfo;
import codegenerator.CommonHelper;
import codegenerator.FileBean;
import codegenerator.JspFileBean;
import codegenerator.TableInfo;

public class InsertGenerator extends JspGenerator {
	
	private static final Logger logger = Log.getLogger();
	private static final String TR_TD = "<tr><td>#columnTitle#</td><td><input type='text' name='#columnName#' id='#columnName#'/></td></tr>";
	private static final String PK_INFO = "#pkInfo#";
	private static final String COLUMN_INFO = "#columnInfo#";
	private static final String PROJECT_NAME = "#projectName#";
	private static final String CLASS_NAME = "#className#";

	@SuppressWarnings("unchecked")
	@Override
	public FileBean createInfo(TableInfo table) throws CodeGeneratorException {
		logger.trace("*** create insert.jsp, table name : " + table.getName());
		String jtmpContent = super.readJtmp("insert.jtmp");
		String projectName = generatorHelper.getProjectName();
		List<ColumnInfo> primaryKeys = table.getPrimaryKey();
		List<ColumnInfo> columns = table.getColumns();
		
		jtmpContent = jtmpContent.replaceAll(PROJECT_NAME, projectName);
		jtmpContent = jtmpContent.replaceAll(PK_INFO, this.createPkInfo(primaryKeys));
		jtmpContent = jtmpContent.replaceAll(COLUMN_INFO, this.createColumnInfo(columns));
		jtmpContent = jtmpContent.replaceAll(CLASS_NAME, CommonHelper.table2ClassName(table.getName()));
		
		FileBean result = new JspFileBean();
		result.setFileName(this.fileName(table));
		result.setContent(jtmpContent);
		result.setPath(generatorHelper.getPageOutputPath());
		return result;
	}
	
	private String createPkInfo(List<ColumnInfo> list) {
		StringBuffer sb = new StringBuffer();
		if(list.size() == 1) {
			sb.append(this.createColumnInfo(list.get(0), false));
		}
		else {			
			for(ColumnInfo column : list) {
				sb.append(this.createColumnInfo(column, true));
				sb.append("\n\t");
			}
		}
		return sb.toString();
	}
	
	private String createColumnInfo(List<ColumnInfo>...lists) {
		StringBuffer sb = new StringBuffer();
		for(List<ColumnInfo> list : lists) {
			for(ColumnInfo column : list) {
				sb.append(this.createColumnInfo(column, false));
				sb.append("\n\t");
			}
		}
		return sb.toString();
	}
	
	/**
	 * 產生tr-td內的資訊
	 *
	 * @param column
	 * @return
	 */
	private String createColumnInfo(ColumnInfo column, boolean isMutiPrimary) {
		String result = TR_TD;
		String columnTitle = StringUtil.isEmptyString(column.getRemark())?column.getName():column.getRemark();
		result = result.replaceAll("#columnTitle#", columnTitle);
		if(isMutiPrimary)
			result = result.replaceAll("#columnName#", "pk." + CommonHelper.column2Attribute(column.getName()));
		else
			result = result.replaceAll("#columnName#", CommonHelper.column2Attribute(column.getName()));
		return result;
	}
	
	/**
	 * 產生.jsp在WEB-INF/jsp/下的資料夾名稱以及檔案名稱
	 *
	 * @param table
	 * @return
	 */
	private String fileName(TableInfo table) {
		return super.fileName(table, "insert");
	}

}
